package com.fushenlan.epidemicControl.service.Impl;

import com.fushenlan.Vo.*;
import com.fushenlan.commons.ApiGlobalErrorDefinition;
import com.fushenlan.commons.GeneralResponse;
import com.fushenlan.commons.ListPagingUtils;
import com.fushenlan.epidemicControl.config.JwtUtil;
import com.fushenlan.epidemicControl.mapper.*;
import com.fushenlan.epidemicControl.service.WorkbenchService;
import com.fushenlan.po.BuildingInfo;
import com.fushenlan.po.Emergency;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author jiajia
 * @DATE 2022/4/21
 * @VERSION 1.0
 */
@Service
public class WorkbenchServiceImpl implements WorkbenchService {


    @Autowired
    private EmergencyMapper emergencyInfoMapper;

    @Autowired
    private BuildingInfoMapper buildingInfoMapper;

    private static final String NEIGHOBRHOOD_ID = "neighborhoodId";

    @Override
    public GeneralResponse<WorkBenchTopDataVO> topData() {
        String userInfo = JwtUtil.getUserInfo(NEIGHOBRHOOD_ID);
        if("".equals(userInfo)){
            return GeneralResponse.fail(ApiGlobalErrorDefinition.TOKEN_EXPIRED);
        }
        WorkBenchTopDataVO workBenchTopDataVO=new WorkBenchTopDataVO();
        int notTransportNum = emergencyInfoMapper.countTransport(userInfo);
        int pengdingTotal =  emergencyInfoMapper.countPending(userInfo);
        workBenchTopDataVO.setPendingEmergency(pengdingTotal);
        workBenchTopDataVO.setPendingTransport(notTransportNum);
        return GeneralResponse.success(workBenchTopDataVO);
    }

    @Override
    public GeneralResponse<ListPagingUtils<WorkbenchNucleinVO>> nucleicInfo(int pageNo,int pageSize) {
        String userInfo = JwtUtil.getUserInfo(NEIGHOBRHOOD_ID);
        if("".equals(userInfo)){
            return GeneralResponse.fail(ApiGlobalErrorDefinition.TOKEN_EXPIRED);
        }
        List<WorkbenchNucleinVO> workbenchNucleinVOS=emergencyInfoMapper.workbenchInfo(userInfo);
        ListPagingUtils<WorkbenchNucleinVO> listPagingUtils=new ListPagingUtils<WorkbenchNucleinVO>(workbenchNucleinVOS,pageSize,pageNo);
        return GeneralResponse.success(listPagingUtils);
    }

    @Override
    public GeneralResponse<ListPagingUtils<WorkBenchEmergencyVO>> emergencyInfo(int pageNo,int pageSize) {
        String userInfo = JwtUtil.getUserInfo(NEIGHOBRHOOD_ID);
        if("".equals(userInfo)){
            return GeneralResponse.fail(ApiGlobalErrorDefinition.TOKEN_EXPIRED);
        }
        Map<String,String> map=new HashMap<>();
        map.put("neighborhoodId",userInfo);
        List<Emergency> list= emergencyInfoMapper.findAllEmergencyList(map);
        List<Long> buildingIds=list.stream().map(Emergency::getBuildingInfoId).distinct().collect(Collectors.toList());
        //未处理小区id 和总计
        Map<Long, Long> pendingMap = list.stream().filter(e->e.getState()==1).collect(Collectors.groupingBy(Emergency::getBuildingInfoId,Collectors.counting()));
        // 未处理小区id和今日新增
        Map<Long, Long> pendingMapToday = list.stream().filter(x->x.getCreateTime().toLocalDate().isEqual(LocalDate.now())).filter(e->e.getState()==1).collect(Collectors.groupingBy(Emergency::getBuildingInfoId,Collectors.counting()));
        //已处理小区id 和总计
        Map<Long, Long> handledMap = list.stream().filter(e->e.getState()==3).collect(Collectors.groupingBy(Emergency::getBuildingInfoId,Collectors.counting()));
        // //已处理小区id 和今日新增
        Map<Long, Long> handledMapTotay = list.stream().filter(x->x.getCreateTime().toLocalDate().isEqual(LocalDate.now())).filter(e->e.getState()==3).collect(Collectors.groupingBy(Emergency::getBuildingInfoId,Collectors.counting()));
        List<WorkBenchEmergencyVO> workBenchEmergencyVOList =new ArrayList<>();
        for(int i=0;i<buildingIds.size();i++){
            long buildingId=buildingIds.get(i);

            WorkBenchEmergencyVO workBenchEmergencyVO =new WorkBenchEmergencyVO();
            //根据小区id查小区名称
            String buildingName="未知小区";
            BuildingInfo buildingInfo=buildingInfoMapper.selectById(buildingId);
            if(buildingInfo!=null){
                buildingName=buildingInfo.getName();
            }

            workBenchEmergencyVO.setBuildingName(buildingName);
            //根据小区id查未处理总计
            workBenchEmergencyVO.setPendingTotal(pendingMap.getOrDefault(buildingId,0L).intValue());
            //根据小区id查今日新增未处理
            workBenchEmergencyVO.setPendingAdd(pendingMapToday.getOrDefault(buildingId,0L).intValue());

            workBenchEmergencyVO.setPendingHistory(workBenchEmergencyVO.getPendingTotal()- workBenchEmergencyVO.getPendingAdd());

            workBenchEmergencyVO.setHandledTotal(handledMap.getOrDefault(buildingId,0L).intValue());

            workBenchEmergencyVO.setHandledAdd(handledMapTotay.getOrDefault(buildingId,0L).intValue());

            workBenchEmergencyVO.setHandledHistory(workBenchEmergencyVO.getHandledTotal()- workBenchEmergencyVO.getHandledAdd());

            workBenchEmergencyVOList.add(workBenchEmergencyVO);
        }
        ListPagingUtils<WorkBenchEmergencyVO> listPagingUtils=new ListPagingUtils<WorkBenchEmergencyVO>(workBenchEmergencyVOList,pageSize,pageNo);
        return GeneralResponse.success(listPagingUtils);
    }


    @Override
    public GeneralResponse<ListPagingUtils<WorkBenchDetailVO>> detail(long residentialId,int pageNo,int pageSize) {

        String userInfo = JwtUtil.getUserInfo(NEIGHOBRHOOD_ID);
        if("".equals(userInfo)){
            return GeneralResponse.fail(ApiGlobalErrorDefinition.TOKEN_EXPIRED);
        }
       List<WorkBenchDetailVO> workBenchDetailVOS=emergencyInfoMapper.workbenchDetail(residentialId,userInfo);
        ListPagingUtils<WorkBenchDetailVO> listPagingUtils=new ListPagingUtils<WorkBenchDetailVO>(workBenchDetailVOS,pageSize,pageNo);

        return GeneralResponse.success(listPagingUtils);
    }
}

